<?php

namespace app\api\controller;

use app\api\model\ConfigSystem as Config;
use app\api\model\VolleyDesc as Balance;
use app\api\model\Speaker as SpeakerModel;
use app\api\model\User;
use GatewayClient\Gateway;

class Speaker extends Common
{
    public function publish()
    {
        return $this->miss('暂时停止发布');

        $match = "/\d{4}$/";

        if (preg_match($match, $this->params['content'])) {
            return $this->miss('不得出现连续数字');
        }

        $config = Config::index('speaker');

        if ($this->member['volley'] < $config['price']) {
            return $this->return['user.error.insufficientBalance'];
        }

        Balance::index($this->member['id'], $config['price'], '发布广场广播');

        SpeakerModel::create([
            'user_id' => $this->member['id'],
            'content' => strip_tags($this->params['content']),
            'create_time' => time(),
        ]);

        $this->lists();

        return $this->return['common.success.submit'];
    }

    public function lists()
    {
        exit;

        $speaker = SpeakerModel::where(['status' => 0])->order('id desc')->find();

        $data = ['code' => 200, 'action' => 'api/Speaker/lists', 'data' => []];
        if ($speaker) {
            if ($speaker['data']) {
                $datas = json_decode($speaker['data'], true);

                $datas['gift_cover'] = changeUrl($datas['gift_cover']);
    
                if ($speaker['types'] == 2) {
                    $datas['large_amount'] = 0;
                    
                    if (isset($datas['gift_price'])) {
                        $config = Config::index('speaker');
                        if ($datas['gift_price'] >= $config['large_amount']) {
                            $datas['large_amount'] = 1;
                        }
                    }
                }

                $data['data'] = $datas;
            }

            $data['data']['types'] = $speaker['types'];
            $data['data']['content'] = $speaker['content'];

            SpeakerModel::update(['id' => $speaker['id'], 'status' => 1]);

            Gateway::sendToAll(json_encode($data,JSON_UNESCAPED_UNICODE));
        }
    }

    public function newest()
    {
        exit;

        $speaker = SpeakerModel::where(['id' => ['gt', 0]])->order('id desc')->find();

        $data = [];
        if ($speaker) {
            if ($speaker['data']) {
                $datas = json_decode($speaker['data'], true);

                $datas['gift_cover'] = changeUrl($datas['gift_cover']);
    
                if ($speaker['types'] == 2) {
                    $datas['large_amount'] = 0;
        
                    if (isset($datas['gift_price'])) {
                        $config = Config::index('speaker');
                        if ($datas['gift_price'] >= $config['large_amount']) {
                            $datas['large_amount'] = 1;
                        }
                    }
                }

                $data = $datas;
            }

            $data['types'] = $speaker['types'];
            $data['content'] = $speaker['content'];
        }

        return $this->pass($data);
    }
}
